<template>
  <div class="video-crop-container">
    <div class="video-container" ref="videoContainerRef">
      <div class="video-real-size" ref="videoWrapper">
        <video
          ref="videoPlayer"
          :src="videoUrl"
          @loadedmetadata="getVideoDimensions"
          class="video"
          :controls="false"
        ></video>
        <VueDragResize
          v-if="crop_box_data.width && crop_box_data.height"
          ref="cropBox"
          :w="crop_box_data.width"
          :h="crop_box_data.height"
          :x="crop_box_data.x"
          :y="crop_box_data.y"
          :minw="30"
          :minh="30"
          parentLimitation
          :is-resizable="true"
          :is-draggable="true"
          @resizing="onResizing"
          @dragging="onDragging"
          class="crop-box"
        >
        </VueDragResize>
      </div>
    </div>
    <div class="crop-ratio-box">
      <div class="video-controls">
        <!-- 播放/暂停控制 -->
        <div class="control-btn" @click="togglePlay">
          <pause-circle-filled v-if="isPlaying" />
          <play-circle-filled v-else />
        </div>
        <!-- 进度条 -->
        <div class="progress-container" ref="progressContainer" @click="seek">
          <div
            ref="startBtnRef"
            class="trim-handle start-handle"
            :style="{ left: starBtntLeft }"
            @mousedown="startDrag"
           
           ></div>
         
        </div>
        <div class="time-display">
          <span class="ml-5"> {{ formatTime(currentTime) }} / {{ formatTime(videoDuration) }}</span>
        </div>
      </div> 
      <div class="flex justify-between align-center  w-full mt-10 py-10">
        <div>
          <span v-if="numUnitValue&&numConsumesValue&&nameValue" class="coast-tip">每{{numUnitValue}}消耗{{pointConsumesValue}}{{pointUnitValue}}{{ nameValue }}, 不足1{{numUnitValue}}按1{{numUnitValue}}计算</span>
        </div>
        <div>
          <a-button
            class="ml-5"
            type="primary"
            @click="startCrop"
            :loading="loading"
            >
           <a-tooltip>
              <template #title>
                暂不支持垂直方向字幕擦除
              </template>
              <question-circle-outlined />
           </a-tooltip>
            开始处理
            </a-button
          >
          <a-button class="ml-5" @click="cancelCrop">取消</a-button>
        </div>
      </div>
    </div>
    <div class="mask" v-if="loading">
      <div class="loading">
        <span>处理中...</span>
      </div>
    </div>
  </div>
</template>
<script setup>
import VueDragResize from "vue-drag-resize/src";
import {  PauseCircleFilled, PlayCircleFilled, QuestionCircleOutlined } from "@ant-design/icons-vue";
import { ref, reactive, nextTick } from "vue";
import { getComputingPowerDetail } from "../utils/index.js";

const props = defineProps({
  videoUrl: {
    type: String,
    default: "",
  },
  loading: {
    type: Boolean,
    default: false,
  },
});
const emit = defineEmits(["cancelCrop", "startCrop"]);
// 获取视频的原始宽高
let videoPlayer = ref();
let videoRealWidth = ref(0);
let videoRealHeight = ref(0);
let videoCssWdith = ref(0);
let videoCssHeight = ref(0);
let videoContainerRef = ref();
let videoDuration = ref(0);
let videoControlRef = ref();
let duration = ref(0);
let currentTime = ref(0);
let progressContainer = ref();
let starBtntLeft  = ref('0%');
let numUnitValue = ref("");
let numConsumesValue = ref("");
let pointConsumesValue = ref("");
let nameValue = ref("");
let pointUnitValue = ref("");

// 新增响应式数据
const videoWrapper = ref(null);

let isPlaying = ref(false);
const crop_box_data = reactive({
  width: 0,
  height: 0,
  x: 0,
  y: 0,
});
let current_ratio = ref(1);

getComputingPowerDetail({key:'extract_video_erasure_point'}).then((res) => {
  if (res) {
    let {
      numberInfo:{unit:numUnit,consumes:numConsumes},
      pointInfo:{unit:pointUnit,consumes:pointConsumes,name}
    } = res; 
    console.log(numUnit,numConsumes,pointConsumes,name,pointUnit);
    
    numUnitValue.value = numUnit;
    numConsumesValue.value = numConsumes;
    pointConsumesValue.value = pointConsumes;
    nameValue.value = name;
    pointUnitValue.value = pointUnit;
  } 
})
// 格式化时间显示
const formatTime = (seconds) => {
  const minutes = Math.floor(seconds / 60);
  seconds = Math.floor(seconds % 60);
  return `${minutes}:${seconds.toString().padStart(2, "0")}`;
};
/**
 * 将CSS尺寸坐标转换到原始视频尺寸坐标系
 * @param {Object} cssCrop  CSS尺寸的裁剪参数 {x, y, width, height}
 * @param {number} videoCssWidth  视频在页面中的实际渲染宽度
 * @param {number} videoCssHeight 视频在页面中的实际渲染高度
 * @param {number} videoRealWidth  视频原始宽度
 * @param {number} videoRealHeight 视频原始高度
 * @returns {Object} 原始视频坐标系中的参数
 */
function convertCropToOriginal(
  cssCrop,
  videoCssWidth,
  videoCssHeight,
  videoRealWidth,
  videoRealHeight
) {
  // 核心计算逻辑
  const scaleX = videoCssWidth / videoRealWidth;
  const scaleY = videoCssHeight / videoRealHeight;

  // 比例容错处理（保护非标准缩放模式）
  if (Math.abs(scaleX - scaleY) > 0.01) {
    console.warn("非等比缩放模式，转换结果可能不准确");
  }

  let scaleX_int = scaleX.toFixed(3);
  let scaleY_int = scaleY.toFixed(3);

  // 计算反向映射
  return {
    x: Math.floor(cssCrop.x / scaleX_int),
    y: Math.floor(cssCrop.y / scaleY_int),
    width: Math.floor(cssCrop.width / scaleX_int),
    height: Math.floor(cssCrop.height / scaleY_int),
  };
}
// 计算最大公约数（GCD）的函数
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
// 将宽高转换为最简比例的公式
const getAspectRatio = (width, height) => {
  const divisor = gcd(width, height);
  return `${width / divisor}:${height / divisor}`;
};
// 比例配置
const ASPECT_RATIO_CONFIG = {
  "1:1": {
    label: "1:1",

    calc: (videoCssWdith, videoCssHeight) => {
      const size = Math.min(videoCssWdith, videoCssHeight);
      return { width: size, height: size };
    },
  },
  "16:9": {
    label: "16:9",

    calc: (videoCssWdith, videoCssHeight) => {
      const size = Math.min(videoCssWdith, videoCssHeight);
      return { width: (size * 16) / 9, height: size };
    },
  },
  "9:16": {
    label: "9:16",
    calc: (videoCssWdith, videoCssHeight) => {
      const size = Math.min(videoCssWdith, videoCssHeight);
      return { width: size, height: (size / 9) * 16 };
    },
  },
  "4:3": {
    label: "4:3",
    calc: (videoCssWdith, videoCssHeight) => {
      const size = Math.min(videoCssWdith, videoCssHeight);
      return { width: (size * 4) / 3, height: size };
    },
  },
  original: {
    label: "原始比例",
    calc: (videoCssWdith, videoCssHeight) => {
      return { width: videoCssWdith, height: videoCssHeight };
    },
  },
  free: {
    label: "自由裁剪",
    calc: (videoCssWdith, videoCssHeight) => {
      const size = Math.min(videoCssWdith, videoCssHeight);
      return { width: size, height: size };
    },
  },
};
const ratioList = ref(
  Object.entries(ASPECT_RATIO_CONFIG).map(([value, config]) => ({
    value,
    label: config.label,
  }))
);

const startCrop = () => {
  const videoCssWdith = videoPlayer.value.offsetWidth;
  const videoCssHeight = videoPlayer.value.offsetHeight;
  const videoRealWidth = videoPlayer.value.videoWidth;
  const videoRealHeight = videoPlayer.value.videoHeight;

  let res = convertCropToOriginal(
    crop_box_data,
    videoCssWdith,
    videoCssHeight,
    videoRealWidth,
    videoRealHeight
  );
  
  emit("startCrop", { ...res });
};
const cancelCrop = () => {
  emit("cancelCrop");
};
const togglePlay = () => {
  if (videoPlayer.value.paused) {
    videoPlayer.value.play();
    isPlaying.value = true;
  } else {
    videoPlayer.value.pause();
    isPlaying.value = false;
  }
};
// 拖动处理
const onDrag = (event) => {
  if (!videoPlayer.value.paused) {
    videoPlayer.value.pause();
    isPlaying.value = false;
  }
  const rect = progressContainer.value.getBoundingClientRect();
  const offsetX = event.clientX - rect.left;
  const newTime = (offsetX / rect.width) * videoDuration.value;
  if (newTime  < videoDuration.value && newTime > 0) {
      starBtntLeft.value = `${(offsetX / rect.width) * 100}%`;
      currentTime.value = newTime;
      videoPlayer.value.currentTime = currentTime.value; // 设置视频播放时间
    }
};
// 停止拖动
const stopDrag = () => {
  document.removeEventListener("mousemove", onDrag);
  document.removeEventListener("mouseup", stopDrag);
  videoPlayer.value.pause();
  isPlaying.value = false;
  // 处理拖动结到视频结尾时间小数点问题
  if(Math.floor(currentTime.value) === Math.floor(videoDuration.value)){
    currentTime.value = videoDuration.value;
    videoPlayer.value.currentTime = currentTime.value;
  }
};
const startDrag = ()=>{
  document.addEventListener("mousemove", onDrag);
  document.addEventListener("mouseup", stopDrag);
}
const ratioChange = (init = false) => {
  initCropBoxData(init);
};
function autoFitContainer(parentWidth, parentHeight, childWidth, childHeight) {
  // 边界检查
  if (parentWidth <= 0 || parentHeight <= 0) {
    throw new Error("Invalid container dimensions");
  }
  if (childWidth <= 0 || childHeight <= 0) {
    return { width: parentWidth, height: parentHeight }; // 返回最大尺寸
  }
  // 计算宽高比
  const containerRatio = parentWidth / parentHeight;
  const childRatio = childWidth / childHeight;
  // 根据宽高比关系动态选择缩放方向
  let targetWidth, targetHeight;

  if (childRatio > containerRatio) {
    // 水平方向填满，垂直居中
    targetWidth = parentWidth;
    targetHeight = parentWidth / childRatio;
  } else {
    // 垂直方向填满，水平居中
    targetHeight = parentHeight;
    targetWidth = parentHeight * childRatio;
  }
  // 返回整数尺寸
  return { width: Math.round(targetWidth), height: Math.round(targetHeight) };
}

const initCropBoxData = async (init = false) => {
  let parentWidth = videoContainerRef.value.offsetWidth;
  let parentHeight = videoContainerRef.value.offsetHeight;
  let videoRealWidth = videoPlayer.value.videoWidth;
  let videoRealHeight = videoPlayer.value.videoHeight;
  let videoAspectString = getAspectRatio(videoRealWidth, videoRealHeight);
  if (init) {
    current_ratio.value = videoAspectString;
    //  如果不是标准视频比例（在ratioList中的比例）
    if (
      ratioList.value.findIndex((item) => item.value == videoAspectString) == -1
    ) {
      current_ratio.value = "free";
    }
  }

  let { width: finalParentWidth, height: finalParentHeight } = autoFitContainer(
    parentWidth,
    parentHeight,
    videoRealWidth,
    videoRealHeight
  );
  videoCssWdith.value = finalParentWidth + "px";
  videoCssHeight.value = finalParentHeight + "px";

  crop_box_data.x = 0;
  crop_box_data.y = 0;
  await nextTick();
  const { width, height } = ASPECT_RATIO_CONFIG[current_ratio.value].calc(
    videoPlayer.value.offsetWidth,
    videoPlayer.value.offsetHeight
  );
  let videoCssWdithValue = Math.floor(width);
  let videoCssHeightValue = Math.floor(height);
  let videoBoundWidth = videoPlayer.value.offsetWidth;
  let videoBoundHeight = videoPlayer.value.offsetHeight;

  let { width: finalWidth, height: finalHeight } = autoFitContainer(
    videoBoundWidth,
    videoBoundHeight,
    videoCssWdithValue,
    videoCssHeightValue
  );
  crop_box_data.width = finalWidth;
  crop_box_data.height = finalHeight;
};

const getVideoDimensions = () => {
  initCropBoxData(true);
  videoDuration.value = videoPlayer.value.duration;
  videoPlayer.value.addEventListener("timeupdate", () => {
    currentTime.value = videoPlayer.value.currentTime;
    starBtntLeft.value = (currentTime.value / videoDuration.value) * 100 + "%";
   
    // 如果视频结束，播放结束
    if (  currentTime.value  >= videoDuration.value) {
      isPlaying.value = false;
      currentTime.value = videoDuration.value;
    }
  });
};
const onResizing = (options) => {
  crop_box_data.x = options.left;
  crop_box_data.y = options.top;
  crop_box_data.width = options.width;
  crop_box_data.height = options.height;
};
const onDragging = (options) => {
  crop_box_data.x = options.left;
  crop_box_data.y = options.top;
  crop_box_data.width = options.width;
  crop_box_data.height = options.height;
};
const handleRatioChange = () => {
  crop_box_data.width = 0;
  crop_box_data.height = 0;
  crop_box_data.x = 0;
  crop_box_data.y = 0;
  ratioChange(false);
};
</script>
<style lang="less" scoped>
.video-crop-container {
  position: relative;
}

.video-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 100%;
  height: 600px;
  background-color: rgba(0, 0, 0, 0.5);
}

.video-real-size {
  position: relative;
  width: v-bind(videoCssWdith);
  height: v-bind(videoCssHeight);
}

.video {
  position: relative;
  width: 100%;
  object-fit: contain;
}

.crop-box {
  outline: 2px dashed #00ff00;
  position: absolute;
  cursor: move;
}

.crop-ratio-box {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.mask {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  font-size: 16px;
  color: #fff;
  inset: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9999;
  cursor: not-allowed;
  .loading {
    color: white;
    width: 120px;
    height: 120px;
    box-sizing: border-box;
    border-radius: 50%;
    border-top: 10px solid #e74c3c;
    position: relative;
    animation: a1 2s linear infinite;
  }
  .loading span {
    width: 120px;
    height: 120px;
    display: block;
    text-align: center;
    line-height: 120px;
    animation: a2 2s linear infinite;
  }
  .loading:before,
  .loading:after {
    content: "";
    width: 120px;
    height: 120px;
    position: absolute;
    left: 0;
    top: -10px;
    box-sizing: border-box;
    border-radius: 50%;
  }
  .loading:before {
    border-top: 10px solid lawngreen;
    transform: rotate(120deg);
  }
  .loading:after {
    border-top: 10px solid var(--ss-primary-color);
    transform: rotate(-120deg);
  }

  @keyframes a1 {
    to {
      transform: rotate(360deg);
    }
  }
  @keyframes a2 {
    to {
      transform: rotate(-360deg);
    }
  }
}
.video-controls {
  width: 100%;
  height: 50px;
  background: linear-gradient(to top, rgba(0,0,0,0.2), rgba(0,0,0,0.6));
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 15px;
  box-sizing: border-box;
  // z-index: 999;
  // margin-top: 3px;

  .control-btn {
    cursor: pointer;
    color: #fff;
    font-size: 28px;
    padding: 5px;
    transition: opacity 0.3s;

    &:hover {
      opacity: 0.8;
    }
  }

  .progress-container {
    flex: 1;
    position: relative;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
    cursor: pointer;
    &::before {
      content: '';
      position: absolute;
      top: 0;
      // transform: translateX(-50%);
      width: v-bind(starBtntLeft);
      height: 100%;
      background: #fff;
      left: 0;
      z-index: 1;
    }
  }

  .trim-handle {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
   
    top: 50%;
    transform: translate(-50%, -50%);
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #fff;
    &:hover {
      opacity: 0.8;
    }
    cursor: all-scroll;
    // &::after {
    //   content: '';
    //   position: absolute;
    //   transform: translateX(-50%);
    //   width: 4px;
    //   height: 4px;
    //   background: #00ff00;
    //   left: 50%;
    //   bottom: 0;
    
    // }
  
  }
 
  .time-display {
    color: #fff;
    font-size: 12px;
  }

  .volume-control {
    display: flex;
    align-items: center;
    gap: 8px;

    span {
      cursor: pointer;
    }

    .volume-slider {
      width: 80px;
      height: 3px;
      background: rgba(255, 255, 255, 0.3);
      appearance: none;

      &::-webkit-slider-thumb {
        appearance: none;
        width: 10px;
        height: 10px;
        background: #fff;
        border-radius: 50%;
      }
    }
  }
}
.coast-tip{
  
  color: #666;
}
</style>
